//
//  B_IntegralViewController.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/8/19.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_IntegralViewController.h"
#import "B_IntegralHeaderCVC.h"
#import "B_IntegralListCVC.h"
#import "B_IntegralDetailVC.h"
#import "B_IntegralDetailListVC.h"
#import "B_IntegralListHeaderCRV.h"
#import "B_IntegralListModel.h"

@interface B_IntegralViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView * collectionView;
@property (nonatomic, strong) B_IntegralListModel * integralModel;
@end

@implementation B_IntegralViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    WeakSelf(weSelf, self);
    [self setTitle:@"我的积分"];
    [self setRightImageBarButtonItemWithFrame:CGRectZero image:@"B_my_points_nav_integral_subsidiary" selectImage:@"" ishidden:YES action:^(GUIButton *button) {
        //TODO:积分详情
        B_IntegralDetailListVC * detailList = [[B_IntegralDetailListVC alloc] init];
        [weSelf pushNextViewController:detailList];
        
    }];
    self.view = self.collectionView;
    self.dataArray = [NSMutableArray array];
    [self getData];
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        //设置区头的大小
        layout.headerReferenceSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, 200);
        
        //设置item大小
        
        layout.itemSize =CGSizeMake((SCREEN_WIDTH - 1) / 2., 135);
        
        //设置区边间距
        
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
        //设置最小间距
        
        [layout setMinimumInteritemSpacing:1];
        
        [layout setMinimumLineSpacing:1];
        
        _collectionView = [[UICollectionView  alloc] initWithFrame:CGRectMake(5, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) collectionViewLayout:layout];
        _collectionView.dataSource = self;
        
        _collectionView.delegate = self;
        
        _collectionView.showsVerticalScrollIndicator = NO;
        
        _collectionView.showsHorizontalScrollIndicator = NO;
        
        [_collectionView registerNib:[UINib nibWithNibName:@"B_IntegralListCVC" bundle:nil] forCellWithReuseIdentifier:@"B_IntegralListCVC"];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"B_IntegralListHeaderCRV" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"B_IntegralListHeaderCRV"];
        
        
        
    }
    return _collectionView;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    B_GoodListModel * model = self.integralModel.goodsListArray[indexPath.row];
    B_IntegralListCVC *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"B_IntegralListCVC" forIndexPath:indexPath];
    [cell.B_goodsImageView sd_setImageWithURL:[NSURL URLWithString:model.goodsImg]];
    cell.B_integralNumber.text = model.integralCount;
    cell.B_goodsTitle.text = model.goodsName;
    
    return cell;
    
    
    
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusableview = nil;
    
    if (kind == UICollectionElementKindSectionHeader){
        
        B_IntegralListHeaderCRV *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"B_IntegralListHeaderCRV" forIndexPath:indexPath];
        headerView.B_integralLabel.text = self.integralModel.integral;
        headerView.B_integralContent.text = @"每日签到, 参与活动及购物均可获得积分";
        reusableview = headerView;
        
    }
    return reusableview;
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake([UIScreen mainScreen].bounds.size.width,200);
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.integralModel.goodsListArray.count;
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
    
}

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //TODO: 执行点击方法
    B_GoodListModel * model = self.integralModel.goodsListArray[indexPath.row];
    B_IntegralDetailVC * detail = [[B_IntegralDetailVC alloc] init];
    detail.B_integralId = model.id;
    [self pushNextViewController:detail];
}


- (void) getData {
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/userIntegral/userIntegral/%@", getUserId()] parameters:nil isCache:NO succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20010"]) {
            weSelf.integralModel = [B_IntegralListModel mj_objectWithKeyValues:data[@"values"]];
            for (NSDictionary * dic in weSelf.integralModel.goodsList) {
                weSelf.integralModel.goodsListArray = [NSMutableArray array];
                B_GoodListModel * model = [B_GoodListModel mj_objectWithKeyValues:dic];
                [weSelf.integralModel.goodsListArray addObject:model];
            }
            [weSelf.collectionView reloadData];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
